<?php
/**
 * 项目管理(代建)工作预告单
 * @author ZhouYP
 **/
namespace Admin\Controller;
use Common\Controller\WahahabaseController;
class WorkPreviewFormController extends WahahabaseController{
    protected $department_model;
    public function _initialize() {
    	parent::_initialize();
    	$this->deal_model = D("Common/WorkPreviewForm");
		$this->att_model = D("Common/Attachment");
		$this->department_model=D("Common/Department");
    }

    /**
     * 查询列表数据
     **/
    public function indexPage($where){
        $eq = array(
            "eq" => "=",
            "neq" => "!=",
            "gt" => ">",
            "egt" => ">=",
            "lt" => "<",
            "elt" => "<=",
            "like" => "LIKE"
        ); 
        // 当前登录人
        $adminId = session('ADMIN_ID');
        $adminGroup = getUserInfo($adminId);

        // $condition = "(
        //     (d.is_publish = 0 
        //         AND 
        //         (d.add_user = $adminId 
        //             OR 
        //             ((SELECT department_id FROM hzzy_users WHERE id=$adminId) = d.add_department)
        //             AND
        //             ((SELECT role_id FROM hzzy_users WHERE id=$adminId) IN('1','2','3'))
        //         )
        //     )
        //     OR
        //     (d.is_publish = 1
        //         AND
        //         (d.add_department= '$adminGroup[department_id]' 
        //             OR 
        //             Com.department_role > '$adminGroup[department_role]' AND '$adminGroup[department_role]' in ('1','2','3')
        //         ) 
        //     )
        //     OR
        //     (d.is_publish = 1
        //         AND
        //         d.executing_department = '$adminGroup[department_id]' 
        //     )
        // )";
        // //权限设置 20171228
        $condition = "(
            (d.is_publish = 0 AND (d.add_user = $adminId OR ((SELECT department_id FROM hzzy_users WHERE id=$adminId) = d.add_department) AND ((SELECT role_id FROM hzzy_users WHERE id=$adminId) IN('1','2','3'))))
            OR
            (d.is_publish = 1   
                AND (
                        (SELECT IFNULL(department_role,0) as `level` FROM hzzy_users u1 LEFT JOIN hzzy_department d1 ON u1.department_id = d1.id WHERE u1.id=$adminId)
                        <= 
                        (SELECT IFNULL(department_role,0) as `level` FROM hzzy_users u2 LEFT JOIN hzzy_department d2 ON u2.department_id = d2.id WHERE u2.id=d.add_user)
                )
                AND  '$adminGroup[department_role]' in ('1','2','3')
                OR ((SELECT department_id FROM hzzy_users WHERE id=$adminId) = d.add_department)
            )
            OR
            ((SELECT role_id FROM hzzy_users WHERE id=$adminId) = 0 AND d.is_publish = 1)
        )";
        foreach($where as $key => $v){
            if(count($v) > 1){
                $d = $eq[$v[0]] == NULL ? '=' : $eq[$v[0]];
                if($d != 'LIKE'){
                    $condition .= " AND $key $d '".$v[1]."'";
                } else {
                    $condition .= " AND $key $d '%".$v[1]."%'";
                }
            } else {
                $condition .= " AND $key = '$v'";
            }
        }
        $count=$this->deal_model->alias("d")
                    ->where($condition)
                    ->join(array(
                        ' LEFT JOIN '.C('DB_PREFIX').'users u ON d.add_user = u.id',
                        ' LEFT JOIN '.C('DB_PREFIX').'department Com ON d.executing_department = Com.id',
                    ))
                    ->count();
        $page = $this->page($count, 20);
        
        $results = $this->deal_model->alias("d")
            ->field('d.*,u.real_name,Com.department_name executing_department')
            ->join(array(
                ' LEFT JOIN '.C('DB_PREFIX').'users u ON d.add_user = u.id',
                ' LEFT JOIN '.C('DB_PREFIX').'department Com ON d.executing_department = Com.id',
            ))
            ->where($condition)
            ->order("d.add_time DESC")
            ->limit($page->firstRow, $page->listRows)
            ->select();
		//exit($this->deal_model->getLastSql());	
        $this->assign("page", $page->show('Admin'));
        $this->assign("results",$results);
        
        $this->display();
    }
	/**
     *  主页
     */
	public function index(){
		/**搜索条件**/
		$where = array("d.is_deleted"=>array('neq', 1));
		$preview_title = trim(I('request.preview_title'));
		if($preview_title){
			$where['preview_title'] = array('like',"%$preview_title%");
		}
		$this->getDepartLevel();
		
        $this->indexPage($where);
	}

	/**
     *  添加
     */
	public function add(){
	    //筛选所有施工单位5级单位
        $Department = $this->department_model->where(array('is_deleted' => 0 , 'department_role'=>5))->field("id,department_name")->order("id DESC")->select();
        $this->assign("data", $Department);
		$this->display();
	}
    public function add_post(){
		$this->addPost(U("WorkPreviewForm/index"));
	}
	
	public function view(){
        //筛选所有施工单位5级单位
        $Department = $this->department_model->where(array('is_deleted' => 0 , 'department_role'=>5))->field("id,department_name")->order("id DESC")->select();
        $this->assign("data", $Department);
		$this->getDepartLevel();
		$this->editDisplay();
	}
	
	/**
	 *  修改
	 */
	public function edit(){
        //筛选所有施工单位5级单位
        $Department = $this->department_model->where(array('is_deleted' => 0 , 'department_role'=>5))->field("id,department_name")->order("id DESC")->select();
        $this->assign("data", $Department);
		$this->editDisplay();
	}
	public function edit_post(){
		$this->editPost(U("WorkPreviewForm/index"));
	}        
	
}


?>